﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WEEK11_上课演示
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        public void BindGridView()
        {
            SqlConnection connection = SqlHelper.GetConnection();
            DataSet dataSet = SqlHelper.GetDataSetBySqlCommand("SELECT * FROM Student", connection);

            gridStudent.DataSource = dataSet;
            Cache["DataSource"] = dataSet;

            gridStudent.DataBind();

            for (int i = 0; i < gridStudent.Rows.Count; i++)
            {
                gridStudent.Rows[i].Cells[0].Text = (i + 1).ToString();
            }
        }

        protected void gridStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridStudent.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridStudent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Buy")
            {
                Response.Write("立刻购买");
            }
            else if (e.CommandName == "AddToCart")
            {
                Response.Write("添加到购物车" + e.CommandArgument);
            }
        }

        protected void gridStudent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                (e.Row.Cells[4].Controls[0] as Button).CommandArgument = e.Row.Cells[2].Text;
            }
        }

        protected void gridStudent_Sorting(object sender, GridViewSortEventArgs e)
        {
        }

        protected void btnSort_Click(object sender, EventArgs e)
        {
            // gridStudent.Sort("StudentID", SortDirection.Descending);
            // BindGridView();
        }
    }
}